package com.joker.web.controller.orange;

import cn.dev33.satoken.annotation.SaIgnore;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.json.JSONUtil;
import com.joker.common.core.controller.BaseController;
import com.joker.common.core.domain.R;
import com.joker.common.helper.LoginHelper;
import com.joker.orange.domain.bo.*;
import com.joker.orange.domain.vo.*;
import com.joker.orange.service.*;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

/**
 * AppOrderController
 *
 * @author Joker
 * @date 2024/6/16 09:15
 */
@Slf4j
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/order")
public class AppOrderController extends BaseController {

    private final IOrderInfoService memberOrderService;
    private final IMemberInfoService memberInfoService;
    private final IUserInfoService userInfoService;
    private final IDiamondRechargeService diamondRechargeService;
    private final AppDeductionOrderService deductionOrderService;

    /**
     * 充值钻石
     */
    @PostMapping("/rechargeDiamond")
    public R<Map<String, Object>> rechargeDiamond(@RequestBody RechargeDiamondBo order) {
        log.info("充值钻石入参【rechargeDiamond】：{}", JSONUtil.parseObj(order));
        DiamondRechargeVo diamondRechargeVo = diamondRechargeService.queryById(order.getDiamondId());
        if (diamondRechargeVo == null) {
            return R.fail("钻石ID为空！");
        }
        // 判断金额是否一致
        if (diamondRechargeVo.getRechargeAmount().compareTo(order.getRechargeAmount()) != 0) {
            return R.fail("充值金额输入错误！");
        }
        Long userId = LoginHelper.getUserId();
        UserInfoVo userInfoVo = userInfoService.queryById(userId);
        return R.ok(memberOrderService.createDiamondOrder(diamondRechargeVo, userInfoVo, order.getPayType()));
    }

    /**
     * 开通会员
     */
    @PostMapping("/playMember")
    public R<Map<String, Object>> playMember(@RequestBody MemberOrderBo memberOrder) {
        log.info("开通会员入参【playMember】：{}", JSONUtil.parseObj(memberOrder));
        MemberInfoVo memberInfoVo = memberInfoService.queryById(memberOrder.getMemberId());
        if (memberInfoVo == null) {
            return R.fail("会员ID为空！");
        }
        // 判断金额是否一致
        if (memberInfoVo.getMemberAmount().compareTo(memberOrder.getMemberAmount()) != 0) {
            return R.fail("开通会员金额输入错误！");
        }
        Long userId = LoginHelper.getUserId();
        UserInfoVo userInfoVo = userInfoService.queryById(userId);
        return R.ok(memberOrderService.createMemberOrder(memberInfoVo, userInfoVo, memberOrder.getPayType()));
    }

    /**
     * 订单回调
     * {
     * "merOrderTid": "1723162461080784896",
     * "tid": "XM4812952343470771467",
     * "money": "499.00",
     * "status": 1,
     * "sign": "24A78993821969CE5FA8C148F21A8CC4"
     * }
     */
    @SaIgnore
    @PostMapping("/orderNotify")
    public R<Void> orderNotify(@RequestBody OrderNotifyBo orderNotifyBo) {
        log.info("【orderNotify】订单回调入参：{}", JSONUtil.parseObj(orderNotifyBo));
        if (ObjectUtil.isNull(orderNotifyBo)) {
            return R.fail("订单回调入参为空");
        }
        Map<String, Object> result = memberOrderService.orderNotify(orderNotifyBo);
        if (ObjectUtil.isNotEmpty(result)) {
            deductionOrderService.DeductionRegister((String) result.get("userId"), (String) result.get("orderNo"));
        }
        return R.ok();
    }

    /**
     * 钻石支付
     */
    @PostMapping("/payOrderByDiamond")
    public R<Void> payOrderByDiamond(@Validated @RequestBody UserBuyOrderBo buyOrderBo) {
        log.info("钻石支付入参【payOrderByDiamond】：{}", JSONUtil.parseObj(buyOrderBo));
        memberOrderService.payOrderByDiamond(buyOrderBo);
        return R.ok();
    }


}
